// pages/coupons/useCoupons/index.js
var common = require("../../../public.js");
import Dialog from '@vant/weapp/dialog/dialog';
Page({
    data: {
        couponsType: '0',
        item: '',
        isGet:''
    },
    back() {
        wx.navigateBack({
            delta: 1,
        })
    },
    receiveStoreConpons(e) {
        let that = this
        let {
            couponid
        } = e.currentTarget.dataset
        let tempJson = {
            userId: that.data.userInfo.id,
            discountCouponId: couponid,
            source: 1, // 1为手动领取，2为系统群发
            token: that.data.token
        }
        common.post("/discountCoupon/getShopCoupon", tempJson, function (res) {
            if (res.status == 200) {
                wx.showToast({
                    title: '领取成功',
                    icon: "none"
                })
                that.setData({
                    storeCouponsList: []
                }, () => {
                    // that.getStoreCoupons(that.data.storeId, true)
                })

            } else {
                wx.showToast({
                    title: res.msg ? res.msg : '领取失败',
                    icon: "none"
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    toUse(e) {
        console.log(e, "buzhis")
        var item = e.currentTarget.dataset.item
        wx.navigateTo({
            url: `/packageA/pages/Goods/storePage/index?id= ${item.storeId}`,
        })
    },
    receiveStoreConpons(e) {
        let that = this
        let {
            couponid
        } = e.currentTarget.dataset
        let tempJson = {
            userId: that.data.userInfo.id,
            discountCouponId: couponid,
            source: 1, // 1为手动领取，2为系统群发
            token: that.data.token
        }
        common.post("/discountCoupon/getShopCoupon", tempJson, function (res) {
            if (res.status == 200) {
                wx.showToast({
                    title: '领取成功',
                    icon: "none"
                })
                that.setData({
                    storeCouponsList: []
                }, () => {
                    that.getStoreCoupons(that.data.storeId, true)
                })

            } else {
                wx.showToast({
                    title: res.msg ? res.msg : '领取失败',
                    icon: "none"
                })
            }
        })
    },
    onLoad: function (opt) {
        let that = this
        common.getLocalUserInfo(function (success, data) {
            if (success) {
                that.setData({
                    userInfo: data.user,
                    token: data.token,
                    openid: data.openid,

                },()=>{
                    console.log(JSON.parse(opt.item),"555555")
                    
                    that.setData({
                        couponsType: opt.type,
                        item: JSON.parse(opt.item),
                        whereFrom: opt.from, //来自哪里  //detail详情已领取，company,未领取
                    },()=>{
                        if(that.data.whereFrom=='detail'){
                           that.setData({
                            isGet:true
                           })
                        }else {
                            that.setData({
                                isGet:false
                               })
             
                        }
                    })
                })
              
            }
        }, true)
     
     
    },
    delectedCoupons() { // 删除该优惠券
        Dialog.confirm({
            title: '删除',
            message: `是否删除"${this.data.item.name}"`
        }).then(() => {
            // on confirm
        }).catch(() => {
            // on cancel
        })
        // common.wxRequest("/discountCoupon", "POST", tempJson, 'application/x-www-form-urlencoded', function (res) {

        // })
    }

})